import {randomUUID} from "node:crypto";

export const dataUrlToFile = (dataUrl: string) => {
    if (!dataUrl) {
        return null;
    }
    const match = dataUrl.match("data:image/(?<fileName>\\w+);base64,(?<file>.+)");
    if (!match || match.groups === undefined) {
        //如果匹配不到，则直接返回空
        return null;
    }

    const extName = match.groups.fileName;
    //解码
    const fileBytes = Buffer.from(match.groups.file, "base64");
    return new File([fileBytes], randomUUID().toString() + `.${extName}`);
}